<?php

/*
	Class: BlogComments

	About: Author
		Jaybill McCarthy

	About: License
		<http://communit.as/docs/license>
*/
class BlogComments extends Blog_Db_Table{

    protected $_name 	= 'blog_comments';
    protected $_primary = 'id';

    function getRecentArray($count = 5, $tags = null) {
    	$out = array();
    	$blog_posts_table = new BlogPosts();
		$cats_table = new BlogViewPostsCategories();

    	if (is_null($tags)) {
    		$comments = $this->fetchAll($this->select()
    			->where('active = 1')
    			->order(array("publish_on desc"))
				->limit($count)
    			);
// dd($comments);
    	} else {
    		$tags_where = $blog_posts_table->getTagWhereClause($tags);    		
    	}

		if (count($comments) > 0) {
			foreach ($comments as $comment) {
				$comment = $this->prepareCommentForView($comment);
				$blog_post = $blog_posts_table->fetchById($comment['post_id']);
				if (!is_null($blog_post)) {
					$comment['post_slug'] = $blog_post->slug;
					$where = $cats_table->getAdapter()->quoteInto("post_id = ?", $blog_post->id);
					$tmp_cats = $cats_table->fetchAllArray($where);
					$cats = array();
					foreach ($tmp_cats as $cat) {
						$tmp_cat = $cat;
						$cat_slug_split = explode("-", $cat['category_slug']);
						$cats[] = array('activity' => $cat_slug_split[0], 'subsection' => $cat_slug_split[1]);
					}
					$comment['post_filed_under'] = $cats;
					$out[] = $comment;
				}
			}
		}
		return $out;
    }

    function prepareCommentForView($comment) {
    	$comment = $comment->toArray();
    	$comment['body'] = stripslashes($comment['body']);
    	$comment['author_full_name'] = stripslashes($comment['author_full_name']);
    	return $comment;
    }

    function prepareRowsetForView($rowset) {
    	$out = array();
    	if (count($rowset) > 0) {
    		foreach ($rowset as $row) {
    			$out[] = $this->prepareCommentForView($row);
    		}
    	}	
    	
    	return $out;
    }
    
}